

from util.selenium.webdriver.common.by import By

from base.BaseMethod import BM
from util.DataQuery.DbTools import DbTools
from util.DataQuery.FormDataDecorator import FormDataDecorator
from PageObject.write_optcond import WriteOptcond


class WriteMaincaseForm():

    form_btn_loc = (By.LINK_TEXT, '主要情况表')
    content_iframe_loc = (By.CSS_SELECTOR, "#innocomFrame")
    mc_domain1_loc = (By.CSS_SELECTOR, 'select#onemain')
    mc_domain2_loc = (By.CSS_SELECTOR, 'select#scdmain')
    mc_domain3_loc = (By.CSS_SELECTOR, 'select#thdmain')
    china_cost_loc = (By.CSS_SELECTOR, 'input#chinaCost')
    basics_cost_loc = (By.CSS_SELECTOR, 'input#basicsCost')
    one_year_total_cost = (By.CSS_SELECTOR, 'input#enterpriseIncome')
    is_event = (By.CSS_SELECTOR, 'input#checkbox_yes')
    no_event = (By.CSS_SELECTOR, 'input#checkbox_no')
    save_btn_loc = (By.CSS_SELECTOR, '.box-footer.text-right button.save_btn')

    # 写代码时方便提示, 基本方法完成后删除
    # driver = driver
    def __init__(self, driver=None):
        self.driver = driver
    @property
    def class_name(self):
        return self.__class__.__name__

    @classmethod
    def get_domain_by_namelist(cls, hitech_field_namelist=""):
        return hitech_field_namelist.split(",")

    def select_tech_info(self, tech_fieldname, tech_fieldlist):
        # domain1, domain2, domain3 = tech_fieldname.split(',')
        if tech_fieldname is None:
            hitech_fields = DbTools.get_hitech_name_by_ids(ids=tech_fieldlist)
            hitech_ = {i.get("id"): i.get("name") for i in hitech_fields}
            tech_fieldname = ",".join([hitech_.get(int(i)) for i in tech_fieldlist.split(",")])
        domain_num = len(tech_fieldname.split(","))
        domain1, domain2, domain3, domain4 = None, None, None, None
        if domain_num == 2:
            domain1, domain2 = self.get_domain_by_namelist(hitech_field_namelist=tech_fieldname)
            domain3 = "无"
            tech_fieldname += ",无"
        if domain_num == 3:
            domain1, domain2, domain3 = self.get_domain_by_namelist(hitech_field_namelist=tech_fieldname)
        elif domain_num == 4:
            domain1, domain2, domain3, domain4 = self.get_domain_by_namelist(hitech_field_namelist=tech_fieldname)

        BM.select_domain(selector_loc=self.mc_domain1_loc, text_value=domain1, driver=self.driver, tech_fieldname=tech_fieldname)
        BM.select_domain(selector_loc=self.mc_domain2_loc, text_value=domain2, driver=self.driver, tech_fieldname=tech_fieldname)
        BM.select_domain(selector_loc=self.mc_domain3_loc, text_value=domain3, driver=self.driver, tech_fieldname=tech_fieldname)

    def input_cost_case(self, inchina_spend, basic_spend, mat_ai, is_events):
        BM.input(ele_loc=self.china_cost_loc, driver=self.driver, keys=inchina_spend)
        BM.input(ele_loc=self.basics_cost_loc, driver=self.driver, keys=basic_spend)
        BM.input(ele_loc=self.one_year_total_cost, driver=self.driver, keys=mat_ai)

        if is_events:
            BM.click(driver=self.driver, ele_loc=self.is_event)
        else:
            # 2000万元以上 ~ 5000万元（含）
            BM.click(driver=self.driver, ele_loc=self.no_event)

    def click_save_btn(self):
        BM.global_click_btn(btn_loc=self.save_btn_loc, driver=self.driver)

    @FormDataDecorator
    def main(self, tech_fieldname='', tech_fieldlist="", inchina_spend=None, basic_spend=None, mat_ai=None, is_events=None):
        BM.click(driver=self.driver, ele_loc=self.form_btn_loc)
        BM.cut_over_frame(driver=self.driver, selector_loc=self.content_iframe_loc)
        BM.wait(driver=self.driver, ele_loc=self.save_btn_loc)
        self.select_tech_info(tech_fieldname=tech_fieldname, tech_fieldlist=tech_fieldlist)

        #  调用数据查询模块之后这里 一般会有三条数据，需要使用 for 循环。
        WriteOptcond(driver=self.driver).write_optcond(category=1, net_assets=9970.35, net_revenues=73512.64, net_income=2604.04)

        self.input_cost_case(inchina_spend=inchina_spend, basic_spend=basic_spend, mat_ai=mat_ai, is_events=is_events)
        self.click_save_btn()
